package com.zhkj.txg.module.mine.adapter

import android.graphics.Paint
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.cart.entity.CartSelectedEntity
import com.zhkj.txg.module.mine.entity.VisitLogEntity
import com.zhkj.txg.utils.ImageUtils

class VisitLogAdapter : BaseQuickAdapter<VisitLogEntity, BaseViewHolder>(R.layout.item_visit_log), LoadMoreModule {

    init {
        addChildClickViewIds(R.id.ivCheck)
        setOnItemChildClickListener { _, view, position ->
            if (view.id == R.id.ivCheck) data[position].selected = !data[position].selected
            notifyItemChanged(position)
        }
    }

    val checkStatusEntity = CartSelectedEntity()
    var editMode: Boolean = false
        set(value) {
            if (field != value) {
                notifyDataSetChanged()
                field = value
            }
        }

    override fun convert(helper: BaseViewHolder, item: VisitLogEntity) {
        val imageView = helper.setText(R.id.tvVisitName, item.goodsName)
            .setText(R.id.tvVisitPrice, SpanUtil.setMoneySymbol(item.price ?: "0.0", true))
            .setGone(R.id.ivCheck, !editMode)
            .setGone(R.id.tvVisitTag, item.isNew != "1")
            .getView<ImageView>(R.id.ivVisit)
        ImageUtils.loadRoundCornerImage(context, imageView, item.image)
        helper.getView<View>(R.id.ivCheck).isSelected = item.selected
        // 设置是显示市场价还是显示分享赚多少钱
        with(helper.getView<TextView>(R.id.tvVisitEarn)) {
            if (AccountManager.isMember() && item.shareEarn.isNotEmpty() && item.shareEarn != "0") {
                setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                paint.flags = Paint.ANTI_ALIAS_FLAG
                text = SpanUtil.setPrefixMoneyStr(price = item.shareEarn)
            } else {
                setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                paint.isAntiAlias = true
                text = SpanUtil.setMoneySymbol(item.marketPrice)
            }
        }
    }

    /**
     * 刷新商品的选中状态
     */
    fun refreshSelectStatus(
        needAllCheck: Boolean = false,
        allCheck: Boolean = false
    ) {
        checkStatusEntity.allSelected = true
        checkStatusEntity.selectAny = false
        for (product in data) {
            // 设置是否全选
            if (needAllCheck) product.selected = allCheck
            // 判断是否全选
            if (!product.selected) checkStatusEntity.allSelected = false
            // 判断是否有选中商品
            else checkStatusEntity.selectAny = true
        }
        if (needAllCheck) notifyDataSetChanged()
    }

    /**
     * 获取选中的item的id,并用,隔开
     */
    fun getSelectedIds(): String {
        return buildString {
            data.forEach { if (it.selected) append(it.id).append(",") }
            if (isNotEmpty()) deleteCharAt(lastIndex)
        }
    }
}